'use strict';
var fs = require("fs");
var path = require('path');
var crypto = require('crypto');
var sqlite3 = require("sqlite3");
var config = require('./conf/config');
var sqlHelper= require('./sqlite3help');
var readline = require('readline'); 

var EventProxy = require('eventproxy');

var initData=function(db,key){
	var initFile="./data/"+key+".json";
	var readStream=fs.createReadStream(initFile);
	const rl = readline.createInterface({
	  input: readStream,
	  crlfDelay: Infinity
	});
	readStream.on('open', function(line) {
		sqlHelper.delByRoot(db,key,function(){
		});
	});
	rl.on('line', (line) => {
		sqlHelper.add(db,JSON.parse(line),function(){
			
		});
	});
	rl.on('close', (line) => {
		console.log(initFile+"文件处理完成.... ");
		fs.unlink(initFile, function(err){
			if(err){
				console.log("err: "+err); 
				return;	
			}
			console.log("删除成功 ");
		});	
		count--;
		if(count<=0){
		 db.run("COMMIT TRANSACTION;");
		}
		console.log("--"+count);
	});
}
var filePaths=config.filePath;
if(filePaths){
	var db = new sqlite3.Database("./data/xfs.db");
	db.run("BEGIN TRANSACTION;");
	var count=0 ;
	for(var key in filePaths) {
		count++;
		initData(db,key); 
	}
}



